﻿function PreparerRoutingDialog(dialogSelector, preparerChangedUrl, assets) {

    var that = this;

    var _dialogSelector = dialogSelector;

    var _messageSelector = $('#PreparerRoutingMessageDialog');

    var _assets = assets;

    function preparerChanged(newPreparerId) {
        $.ajax({
            cache: false,
            data: {
                jeId: $('#jeGuidPreparerRouting').val(),
                preparerId: newPreparerId
            },
            url: preparerChangedUrl,
            success: handlePreparerChangedResponse
        });
    }

    that.setupDialog = function (selector) {
        selector.dialog(
            {
                autoOpen: false,
                modal: true,
                resizable: false,
                width: 'auto',
                height: 'auto',
                maxHeight: 400,
                zIndex: 10000,
                open: function () {
                    // recenter dialog on open
                    _dialogSelector.dialog('widget').position({ my: "center", at: "center", of: window });
                    that.configureButtonEvents();
                }
            }
        );
    };

    that.setupMessageDialog = function () {
        _messageSelector = $('#PreparerRoutingMessageDialog');
        var buttonArray = {};
        buttonArray["OK"] = function () {
            that.closeMessageDialog();
        };
        _messageSelector.dialog(
            {
                autoOpen: false,
                modal: true,
                resizable: false,
                width: 250,
                buttons: buttonArray,
                zIndex: 10000,
                close: function () {
                    $('#PreparerRoutingMessageDialog').empty();
                    $('#PreparerRoutingMessageDialog').dialog('destroy');
                }
            }
        );
    };

    that.openMessageDialog = function (message, title) {
        that.setupMessageDialog();

        if (title)
            _messageSelector.dialog('option', 'title', title);

        _messageSelector.html(message);

        _messageSelector.dialog('open');
    };

    that.closeMessageDialog = function () {
        _messageSelector.dialog('close');
    };

    that.openDialog = function (view) {
        _dialogSelector.html(view);
        _dialogSelector.dialog('open');
    };

    that.closeDialog = function () {
        _dialogSelector.dialog('close');
    };

    that.handleResponse = function (data) {
        if (data.success) {
            that.closeDialog();
            if (data.refreshPage) {
                location.reload();
            }
        } else {
            that.openMessageDialog(data.errorMessage, _assets.titles.error);
        }
    };

    function handlePreparerChangedResponse(data) {
        if (data.SpecialBehaviourMessage)
            that.openMessageDialog(data.SpecialBehaviourMessage);
    }

    that.configureButtonEvents = function () {
        $('.preparer-routing-cancel').click(function () {
            that.closeDialog();
        });
        $('#CurrentPreparers_0__Id').change(function () {
            preparerChanged($(this).find(":selected").val());
        });
    };

    that.setupDialog(_dialogSelector);

    return that;
}